﻿using System;
using System.Linq;

namespace RayDen.Library.Core.Primitives {
    [Serializable]
    public struct UV : ISerializableValue{
        public float U, V;
        public UV( float u, float v ) {
            U = u;
            V = v;
        }

        public static UV operator *( float f, UV v ) {
            return new UV(v.U * f, v.V * f);
        }
        public static UV operator +( UV v, UV f ) {
            return new UV(v.U + f.U, v.V+ f.V);
        }

        public static UV operator *( UV v, float f ) {
            return new UV(v.U*f, v.V*f);
        }

        public float[] ToArray() {
            return new [] { U, V};
        }

        public byte[] Serialize()
        {
            return new[]
                {
                    BitConverter.GetBytes(U),
                    BitConverter.GetBytes(V),
                }.SelectMany(t => t).ToArray();
        }

        public void Deserialize(byte[] data, int offset = 0)
        {
            U = BitConverter.ToSingle(data, 0+offset);
            V = BitConverter.ToSingle(data, 4+offset);
        }
    }
}
